﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Structure.Interfaces;
using Structure.Objects;
using Command;

namespace Application
{
    public partial class ProjectForm : System.Web.UI.UserControl
    {
        public event Action OnAdd;  
        private iCommand _command = BuildCommand.Build();   

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //    ProjectId = String.Empty;
        }

       
        protected void btAddProject_Click(object sender, EventArgs e)
        {
            int ManagerId = 0;

            Project p = new Project(NewProjectName.Text, ManagerId, NewProjectStartDate.Text, NewProjectEndDate.Text); 

            if( String.IsNullOrEmpty(ProjectId.Text))
                _command.AddProject(p);
            else
                _command.UpdateProject(ProjectId.Text,p);

            ProjectFormDiv.Visible = false;
            NewProjectName.Text = "";
            NewProjectStartDate.Text = "";
            NewProjectEndDate.Text = "";

            OnAdd();
        }

        protected void addProject_Click(object sender, EventArgs e)
        {
            ProjectId.Text = String.Empty;
            ProjectFormDiv.Visible = true;
        }

        protected void ProjectFormClose_Click(object sender, ImageClickEventArgs e)
        {
            ProjectFormDiv.Visible = false;
        }

        public void Edit(string id)
        {
            Project p = _command.GetProject(id);

            NewProjectName.Text = p.Name;
            NewProjectEndDateCalendar.SelectedDate = Convert.ToDateTime(p.StartDate);
            NewProjectStartDateCalendar.SelectedDate = Convert.ToDateTime(p.EndDate);
       //NewProjectStartDate.Text = p.StartDate;
       //     NewProjectEndDate.Text = p.EndDate;    
            ProjectId.Text = id;

            ProjectFormDiv.Visible = true;
        }

       
       
    }
}